﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PPP.ViewModel;
using NLog;
using Fluent;
using Xceed.Wpf.AvalonDock;
using Xceed.Wpf.AvalonDock.Layout;
using iTuner;
using PPP.ViewModel.WindowModel;


namespace PPP {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow {

        private readonly Logger _logger = LogManager.GetLogger("MainWindow");       
        
        public MainWindow() {

            _logger.Debug("Start ... initialize component.");

            InitializeComponent();

            _logger.Debug("Create new MainViewModel context.");
            
            //this.DataContext = new MainViewModel(this, new Services.HardDrivePicturesService(@"C:\Users\admin\Documents\Visual Studio 2013\Projects\PPP\PPP\Images\Samples",true));
            this.DataContext = new MainViewModel(this, new Services.HardDrivePicturesService(@"C:\", false));
            
            

            this.Loaded += new RoutedEventHandler(Window_Loaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(Window_Closing);
          	                   
        }

        #region Load/Unload Initial Layout
        
        #endregion

        #region Dock Methods

       
        #endregion

        #region Methods

        public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject {
            if (depObj != null) {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T) {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child)) {
                        yield return childOfChild;
                    }
                }
            }
        }
        
        #endregion

        #region Events

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            //ScreenTip.HelpPressed += OnScreenTipHelpPressed;
        }

        private void dockingManager_Loaded(object sender, RoutedEventArgs e) {
           
        }
        
        private void dockManager_DocumentClosed(object sender, EventArgs e) {
        }
       
        /// <summary>
        /// When the active content changed then call the select ribbon tab item command which select/open the correct ribbon tab.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dockingManager_ActiveContentChanged(object sender, EventArgs e) {

            ((MainViewModel)this.DataContext).SelectRibbonTabItemCommand.Execute(new List<object>{ sender, ribbonView.ribbon });
        }

        /// <summary>
        /// When the gallery size changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageGallery_SizeChanged(object sender, SizeChangedEventArgs e) {
            ((MainViewModel)this.DataContext).SelectedGallery.GallerySize = Convert.ToInt32(((ListBox)sender).ActualWidth - 200);
        }

        #endregion        

    }
}
